<?php defined("MAIN_DIR") or die("No direct access");

$photos = $db->getTable("select `file`,`text` from `photos` where `switch` = '1' order by `order`,`id`");

require_once APP.HEADFILE;
?>
<script type="text/javascript">
var images;
var sw_time = 1000;
var active = 0;
var timer;

function showText (obj,text,num,time){
	if (num == 0){
		obj.text("");
	}
	var str = obj.text();
	
	if (num < text.length){
		str = str.substr(0,str.length-1);
		setTimeout(function(){
			obj.text(str+text[num]+(num%2 == 0 ? "-" : "_"));
			showText(obj,text,num+1,time);
		},time);
	} else {
		obj.text(obj.text(str.substr(0,str.length-1)));
	}
}

function nextPhoto(){
	var next = (images.length-1 <= active) ? 0 : active + 1;
	$(images[active]).fadeOut(sw_time);
	$(images[next]).fadeIn(sw_time);
	active = next;
	showText($("#photoText"),$(images[active]).attr("alt"),0,30);
	clearInterval(timer);
	timer = setInterval(nextPhoto,5000);
}
function prevPhoto(){
	var prev = (active <= 0) ? images.length-1 : active - 1;
	$(images[active]).fadeOut(sw_time);
	$(images[prev]).fadeIn(sw_time);
	active = prev;
	showText($("#photoText"),$(images[active]).attr("alt"),0,30);
	clearInterval(timer);
	timer = setInterval(nextPhoto,5000);
}
$(document).ready(function(){
	images = $("#photo-album img");
	active = images.length - 1;
	timer = setInterval(nextPhoto,5000);
	nextPhoto();
	$("#prevPhoto").click(function(){
		prevPhoto();
	});
	$("#nextPhoto").click(function(){
		nextPhoto();
	});
});
</script>

<div id="photo-album">
	<div id="prevPhoto"></div>
	<?php
		foreach ($photos as $photo) {
			echo "<img src='/files/img/photos/x_".$photo['file']."' alt='{$photo['text']}' />\n";
		}
	?>
	<div id="nextPhoto"></div>
	<div id="photoText"></div>
</div>

<?php require_once APP.FOOTERFILE; ?>